package cn.tedu.erp.project.controller;

import cn.tedu.erp.project.pojo.dto.FinancePayrollAddNewDTO;
import cn.tedu.erp.project.pojo.dto.FinancePayrollUpdateDTO;
import cn.tedu.erp.project.pojo.vo.FinanceFixedAssetsStandardVO;
import cn.tedu.erp.project.pojo.vo.FinancePayrollListVO;
import cn.tedu.erp.project.pojo.vo.FinancePayrollStandardVO;
import cn.tedu.erp.project.service.IFinancePayrollService;
import cn.tedu.erp.project.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@Api(tags="工资核算表模块")
@RequestMapping("/financePayrolls")
@RestController
public class FinancePayrollController {
    @Autowired
    private IFinancePayrollService service;
    // http://localhost:8080/FinancePayroll/add-new?name=test001&description=hahahaha&sort=100
    // http://localhost:8080/FinancePayroll/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加工资核算表")
    @PreAuthorize("hasAnyAuthority('/erp_maindb/financePayrolls/add-new')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid FinancePayrollAddNewDTO financePayrollAddNewDTO) {
        log.debug("开始处理【添加工资核算表】的请求业务，参数：{}", financePayrollAddNewDTO);
        service.addNew(financePayrollAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8080/FinancePayroll/delete?id=1
    @PostMapping("/{id}/delete")
    @PreAuthorize("hasAnyAuthority('/erp_maindb/financePayrolls/delete')")
    @ApiOperation("根据id删除工资核算表")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value ="工资核算表id",required = true,dataType = "Long"),

    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1,message ="请提交有效的id" ) Long id) {
        service.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询工资核算表")
    @PreAuthorize("hasAnyAuthority('/erp_maindb/financePayrolls/read')")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<FinancePayrollListVO>> list(){
        log.debug("开始处理【查询列表】的请求，无参数");
        List<FinancePayrollListVO> list = service.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/financePayrolls/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询工资核算表详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<FinancePayrollStandardVO> getStandardById(
            @PathVariable Long id) {
        log.debug("开始处理【根据ID查询工资核算表详情】的请求，参数：{}", id);
        FinancePayrollStandardVO queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAnyAuthority('/erp_maindb/financePayrolls/update')")
    @ApiOperation("修改工资核算表")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value ="工资核算表id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "username",value ="用户名" ,required = true,dataType = "String")
    })
    public JsonResult<Void> update(@PathVariable@Range(min = 1,message ="请提交有效的id" ) Long id,
                                   @Valid FinancePayrollUpdateDTO financePayrollUpdateDTO) {
        service.updateInfoById(id,financePayrollUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/financePayrolls/list-by-empId
    @ApiOperation("根据月份查询职员")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParam(name = "employeeId", value = "职员id",
            required = true, dataType = "long")
    @GetMapping("/list-by-empId")
    public JsonResult<List<FinancePayrollListVO>> listByEmpId(Long employeeId) {
        log.debug("开始处理【根据属性模板查询属性列表】的请求，参数：{}", employeeId);
        List<FinancePayrollListVO> list = service.listByEmpId(employeeId);
        return JsonResult.ok(list);
    }
}
